<?php
/**
 * Indetification Validator
 * @author Redouane
 * @since  20/04/2011
 */
class bay3ValidatorLogin extends sfValidatorBase
{
  public function configure($options = array(), $messages = array())
  {
    $this->addOption('email_field', 'email');
    $this->addOption('password_field', 'password');
    $this->addOption('status_field', false);
    $this->addMessage('status_field', 'Votre compte est désactivé');
  }

  protected function doClean($values)
  {
    $login   = isset($values[$this->getOption('email_field')]) ? $values[$this->getOption('email_field')] : '';
    $password  = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';
    
    // Login & password is set ?
    if (empty($login)) return false;
    if (empty($password)) return false;
    
    // visitor exists ?
    $seller = Doctrine_Core::getTable("Seller")->findOneByEmailAndPassword($login, $password);
    if ($seller instanceof Seller){
    	if($seller->status == 1)
    		return array_merge($values, array('email' => $login));
    	else 
    		//throw new sfValidatorErrorSchema($this, array($this->getOption('email_field') => new sfValidatorError($this, 'invalid')));
    		throw new sfValidatorError($this, 'status_field');
    }else{
    	throw new sfValidatorErrorSchema($this, array($this->getOption('email_field') => new sfValidatorError($this, 'invalid')));
    }	
  }
}
